package org.jeecg.modules.innerApi.leqiApi.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.common.util.MathUtil;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.innerApi.common.responseDto.InvoiceRtn;
import org.jeecg.modules.innerApi.leqiApi.convert.LQRequestParamConvert;
import org.jeecg.modules.innerApi.leqiApi.convert.LQResponseRtnConvert;
import org.jeecg.modules.innerApi.leqiApi.requestVo.LQInvoiceParam;
import org.jeecg.modules.innerApi.leqiApi.responseVo.LQApiResponse;
import org.jeecg.modules.innerApi.leqiApi.service.ILeqiService;
import org.jeecg.modules.innerApi.leqiApi.util.LQRequestUtil;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LeqiServiceImpl implements ILeqiService {
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private LQRequestUtil lqRequestUtil;

    @Override
    public InvoiceRtn invoice(InvoiceParam invoiceParam) {
        //String sdjk_use_test = sysDictService.queryDictTextByKey("xxparams", "sdjk_use_test");
        LQInvoiceParam param = LQRequestParamConvert.invoiceParamConvert(invoiceParam);
        LQApiResponse response = null;
        //if (!"1".equals(sdjk_use_test)) {//是否模拟数据在乐企接口服务中控制
            response = lqRequestUtil.request(JSON.toJSONString(param));
        /*} else {
            //cwgx todo:乐企模拟数据
            JSONObject rtnDataJson = new JSONObject();
            rtnDataJson.put("ywqqlsh",invoiceParam.getFpsqlsh());
            rtnDataJson.put("ywlx",invoiceParam.getSjlx());
            JSONObject responseObject = new JSONObject();
            responseObject.put("RequestId", IdWorker.getIdStr());
            responseObject.put("RequestId", IdWorker.getIdStr());
            JSONObject dataObject = new JSONObject();

            dataObject.put("returncode","00");
            dataObject.put("returnmsg","成功");
            dataObject.put("fpdm","");
            dataObject.put("fphm","234120000000" + RandomUtil.randomNumbers(8));
            dataObject.put("kprq", DateUtils.now());
            responseObject.put("Data", dataObject.toJSONString());

            rtnDataJson.put("Response", responseObject);

            response = JSON.toJavaObject(rtnDataJson, LQApiResponse.class);
        }*/
        InvoiceRtn invoiceRtn = LQResponseRtnConvert.fpkjRtnConvert(response);
        if (invoiceRtn.getSuccess()) {
            invoiceRtn.getData().setFpsqlsh(invoiceParam.getFpsqlsh());
            invoiceRtn.getData().setFplxdm(invoiceParam.getFplxdm());
            invoiceRtn.getData().setHjje(invoiceParam.getHjje());
            invoiceRtn.getData().setHjse(invoiceParam.getHjse());
            invoiceRtn.getData().setJshj(MathUtil.add(invoiceParam.getHjje(), invoiceParam.getHjse()));
        }
        return invoiceRtn;
    }
}
